<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="办公楼名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入办公楼名称" :clearable="true" />
      </el-form-item>
      <el-form-item label="办公区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请输入办公区" :clearable="true">
          <el-option v-for="dict in areaList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['office:building:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['office:building:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="OfficeBuilding"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['office:building:update']"
        />
      </template>
    </Table>
  </ContentWrap>
  <OfficeBuildingEdit ref="editFormRef" @success="getPage" />
</template>
<script setup name="OfficeBuilding" lang="ts">
import * as OfficeBuildingApi from '@/api/office/building'
import OfficeBuildingEdit from './OfficeBuildingEdit.vue'
import { cloneDeep } from 'lodash-es'
const editFormRef = ref()
const areaList = ref()
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  areaId: undefined
})
const queryData = ref()
const columns = [
  {
    label: '办公楼名称',
    prop: 'name'
  },
  {
    label: '办公区',
    prop: 'area.name'
  },
  {
    label: '建筑面积',
    prop: 'buildingArea'
  },
  {
    label: '办公用房建筑面积',
    prop: 'officeBuildingArea'
  },
  {
    label: '总楼层',
    prop: 'floorCount'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  },
  {
    label: '更新时间',
    prop: 'updateTime'
  }
]
/** 初始化 */
onMounted(async () => {
  getPage()
})

onActivated(() => {
  if (history.state.areaId != undefined) {
    queryParams.value.areaId = cloneDeep(history.state.areaId)
    delete history.state.areaId
  }
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  editFormRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await OfficeBuildingApi.pageQuery(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  editFormRef.value.open('update', row.id)
}
</script>
